//------------------------------------------------
// Coarse Theme Manage Tool
// Author : Alex Woo Hyun Kim
// Date : Aug. 9 , 2002
// Email : woorung@empal.com
//------------------------------------------------

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <mysql.h>
#include "CWnCgi.h"

int main()
{
	cout << "Content-type: text/html\n\n";
	cout << "<html><body>\n";

	CWnCgi *WnCgi = new CWnCgi();

	if(WnCgi->getFormData()==true) {
		char szDefaultImagePath[] = "/home/hanmir/public_html/ed_images/tmp/";
		char szDefaultHtmlPath[] = "/data1/SEARCH/HPDATA/DATA/ED_DOC/tmp/";
		char *pszImgFileName;
		char *pszHtmlFileName;
		char szUploadImgFileName[1000];
		char szUploadHtmlFileName[1000];
		char szImgParam[256];
		char szHtmlParam[256];

		pszImgFileName = WnCgi->getEntryFilenameByEntryName("imgfile1");
		pszHtmlFileName = WnCgi->getEntryFilenameByEntryName("htmlfile1");

		if(pszImgFileName!=NULL||pszHtmlFileName!=NULL) {
			for(int i=1,k=1;i<=WnCgi->getEntryNum();k++,i+=2) {
				sprintf(szImgParam,"imgfile%d",k);
				pszImgFileName = WnCgi->getEntryFilenameByEntryName(szImgParam);
				sprintf(szHtmlParam,"htmlfile%d",k);
				pszHtmlFileName = WnCgi->getEntryFilenameByEntryName(szHtmlParam);
				if(pszImgFileName==NULL||pszHtmlFileName==NULL) break;
				sprintf(szUploadImgFileName,"%s%s",szDefaultImagePath,pszImgFileName);
				sprintf(szUploadHtmlFileName,"%s%s",szDefaultHtmlPath,pszHtmlFileName); 
				if(WnCgi->writeToFileFromEntryData(szImgParam,szUploadImgFileName)&&WnCgi->writeToFileFromEntryData(szHtmlParam,szUploadHtmlFileName)) {
                	cout << "<table width=602 border=0 cellspacing=0 cellpadding=1 align=center>" << endl;
                	cout << "<tr><td bgcolor=#8080ff>" << endl;
                	cout << "<table width=600 border=0 cellspacing=1 cellpadding=10 align=center>" << endl;
                	cout << "<tr>" << endl;
                	cout << "<td width=600 colspan=2 align=center bgcolor=#cfd0ed><font size=2><b>completed Image upload : " << szUploadImgFileName << "</b></font></td>" << endl;
                	cout << "</tr>" << endl;
					cout << "<td width=600 colspan=2 align=center bgcolor=#cfd0ed><font size=2><b>completed Html upload : " << szUploadHtmlFileName << "</b></font></td>" << endl;
                	cout << "<tr>" << endl;
                	cout << "<td width=500 bgcolor=#fafaee>" << endl;

					cout << "<center>" << endl;
					cout << "<img src=\"/ed_images/tmp/" << WnCgi->toHanguelFileName(pszImgFileName) << "\">" << endl;	
					cout << "</center>" << endl;

					cout << "</td>" << endl;
                	cout << "</tr>" << endl;
                	cout << "</table>" << endl;
					cout << "</tr>" << endl;
					cout << "</table>" << endl;
					cout << "<br>" << endl;
				}
				else {
					cout << "<center>" << endl;
					cout << "!! failed upload : " << szUploadImgFileName << endl;
					cout << "</center>" << endl;
					cout << "<br>" << endl;
				}
			}
		}
		else {
			char *pszImageCount = WnCgi->getEntryDataByEntryName("imgcount");
			char *pszHtmlCount = WnCgi->getEntryDataByEntryName("htmlcount");
			int nImageCount = atoi(pszImageCount);
			int nHtmlCount = atoi(pszHtmlCount);	

			cout << "<form method=\"post\" enctype=\"multipart/form-data\" action=\"ThemeMan.cgi\">" << endl;
            cout << "<table width=602 border=0 cellspacing=0 cellpadding=1 align=center>" << endl;
            cout << "<tr><td bgcolor=#8080ff>" << endl;
            cout << "<table width=600 border=0 cellspacing=1 cellpadding=10 align=center>" << endl;
            cout << "<tr>" << endl;
            cout << "<td width=600 colspan=2 align=center bgcolor=#cfd0ed><font size=2><b>Coarse Theme Management Tool</b></font></td>" << endl;
            cout << "</tr>" << endl;

			for(int i=1;i<=nImageCount;i++) {
              		cout << "<tr>" << endl;
              		cout << "<td width=100 align=center bgcolor=#fafaee><font size=2>Img File " << i << "</font></td>" << endl;
              		cout << "<td width=500 bgcolor=#fafaee><input type=file name=imgfile" << i << " size=40></td>" << endl;
              		cout << "</tr>" << endl;

					cout << "<tr>" << endl;
					cout << "<td width=100 align=center bgcolor=#fafaee><font size=2>HTML File " << i << "</font></td>" << endl;
					cout << "<td width=500 bgcolor=#fafaee><input type=file name=htmlfile" << i << " size=40></td>" << endl;
					cout << "</tr>" << endl;
			}


            cout << "<tr>" << endl;
            cout << "<td align=center colspan=2 bgcolor=#cfd0ed>" << endl;
            cout << "<font size=2><input type=submit value=Upload></font>" << endl;
            cout << "</td>" << endl;
            cout << "</tr>" << endl;
            cout << "</table>" << endl;
            cout << "</tr>" << endl;
            cout << "</table>" << endl;
            cout << "</form>" << endl;
 
            cout << "<hr width=602>" << endl;
            cout << "<center>" << endl;
            cout << "<form method=\"post\" action=\"ThemeMan.cgi\">" << endl;
            cout << "[count of image] : <input type=text name=imgcount size=5 value=" << pszImageCount << ">" << endl;
           	cout << "[count of html] : <input type=text name=htmlcount size=5 value=" << pszHtmlCount << ">" << endl;
			cout << "<input type=submit value=Reload>" << endl;
           	cout << "</form>" << endl;
            cout << "</center>" << endl;
		}
	}
	else {
		cout << "<form method=\"post\" enctype=\"multipart/form-data\" action=\"ThemeMan.cgi\">" << endl;
		cout << "<table width=602 border=0 cellspacing=0 cellpadding=1 align=center>" << endl;
		cout << "<tr><td bgcolor=#8080ff>" << endl;
		cout << "<table width=600 border=0 cellspacing=1 cellpadding=10 align=center>" << endl;
		cout << "<tr>" << endl;
		cout << "<td width=600 colspan=2 align=center bgcolor=#cfd0ed><font size=2><b>Corse Theme Management Tool</b></font></td>" << endl;
		cout << "</tr>" << endl;
		cout << "<tr>" << endl;
		cout << "<td width=100 align=center bgcolor=#fafaee><font size=2>Img File 1</font></td>" << endl;
		cout << "<td width=500 bgcolor=#fafaee><input type=file name=imgfile1 size=40></td>" << endl;
		cout << "</tr>" << endl;
		cout << "<tr>" << endl;
		cout << "<td width=100 align=center bgcolor=#fafaee><font size=2>HTML File 1</font></td>" << endl;
		cout << "<td width=500 bgcolor=#fafaee><input type=file name=htmlfile1 size=40></td>" << endl;
		cout << "</tr>" << endl;
		cout << "<tr>" << endl;
		cout << "<td align=center colspan=2 bgcolor=#cfd0ed>" << endl;
		cout << "<font size=2><input type=submit value=Upload></font>" << endl;
		cout << "</td>" << endl;
		cout << "</tr>" << endl;
		cout << "</table>" << endl;
		cout << "</tr>" << endl;
		cout << "</table>" << endl;
		cout << "</form>" << endl;

		cout << "<hr width=602>" << endl;
		cout << "<center>" << endl;
		cout << "<form method=\"post\" action=\"ThemeMan.cgi\">" << endl;
                cout << "[count of image] : <input type=text name=imgcount size=5 value=1>" << endl;
                cout << "[count of html] : <input type=text name=htmlcount size=5 value=1>" << endl;
		cout << "<input type=submit value=Reload>" << endl;
		cout << "</form>" << endl;
		cout << "</center>" << endl;
	}

	delete WnCgi;

	cout << "</body></html>" << endl;
	return 0;
}
